package com.flyee.controller.tools.sandtable{
	
	import alternativa.engine3d.controllers.*;
	import alternativa.engine3d.core.*;
	import alternativa.types.*;
	
	import com.flyee.utils.gs.TweenMax;
	
	import flash.display.*;
	import flash.events.*;

	public class MouseDrager extends Sprite {


		private var myCamera:Camera3D;
		private var myOb3d:Object3D;
		private var control:CameraController;
		public var objs : Array;

		private var mouseDownX:Number;
		private var mouseDownY:Number;
		private var mouseNowX:Number;
		private var mouseNowY:Number;
		private var isMouseDown:Boolean=false;
		
//		public var stg : Cat;
		
		public function MouseDrager(mCamera:Camera3D=null,ob3d:Object3D=null, ctrl:CameraController=null):void {
			myCamera=mCamera;
			myOb3d=ob3d;
			control = ctrl;
			myCamera.y=hMin;
		}
		private var zz:Number=1200;
		private var hMin:Number=10;
		private var hMax:Number=900;
		private var due:int=2;

		public function process():void {
			if (myCamera.y<hMin) {
				myCamera.y=hMin;
			}
			else if (myCamera.y>hMax) {
				myCamera.y=hMax;
			}
//			myCamera.y=zz-myCamera.z*(hMax/zz);
			control.lookAt(new Point3D(0, 0, 0));
		}
		public function onMouseClick(evt:MouseEvent):void {
			if (evt.type=="mouseDown") {
				isMouseDown=true;
				mouseDownX=mouseX;
				mouseDownY=mouseY;
//				stg.stage.quality = StageQuality.LOW;
			}
			if (evt.type=="mouseUp") {
				isMouseDown=false;
//				stg.stage.quality = StageQuality.HIGH;
			}
			if (evt.type=="mouseOut") {
				isMouseDown=false;
			}
			if (evt.type=="mouseMove") {
				mouseNowX=mouseX;
				mouseNowY=mouseY;
				if (isMouseDown) {
					TweenMax.to(myOb3d, 2, {rotationZ:myOb3d.rotationZ+(mouseDownX-mouseNowX)/50, delay:0});
					for each(var obj3d : Object3D in objs){
//						obj3d.rotationZ = myOb3d.rotationZ;
						TweenMax.to(obj3d, 2, {rotationZ:obj3d.rotationZ-(mouseNowX-mouseDownX)/50, delay:0});
					}
				}
				if (isMouseDown) {
//					var num : Number = myCamera.z-(mouseDownY-mouseNowY)*100;
//					if(num>=0)
//						TweenMax.to(myCamera, 3, {z:myCamera.z-(mouseDownY-mouseNowY)*100, delay:0});
//					TweenMax.to(myOb3d, 1, {rotationY:myOb3d.rotationY+(mouseNowY-mouseDownY)/100, delay:0});
				}
			}

		}
		
		public function updateDistance(distance : Number) : void{
//			myCamera.moveBackward(distance);
//			TweenMax.to(myOb3d, 1, {scale:myOb3d.scale+distance, delay:0});
		}
		
		public function dispose() : void {
			myCamera = null;
			myOb3d = null;
			control = null;
			objs = null;
		}
	}
}